﻿namespace Digitalmes.Application.Sys.Roles.Queries;

/// <summary>
/// 查询角色命令。
/// </summary>
public record GetRoleListQuery(string? Name, string? Code) : IRequest<IResult<List<SysRoleDto>>>;

internal sealed class GetRoleListQueryHandler(IRepository<SysRole> repo) : IRequestHandler<GetRoleListQuery, IResult<List<SysRoleDto>>>
{
    public async Task<IResult<List<SysRoleDto>>> Handle(GetRoleListQuery request, CancellationToken cancellationToken)
    {
        var items = await repo.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(request.Name), m => m.Name.Contains(request.Name!))
            .WhereIF(!string.IsNullOrEmpty(request.Code), m => m.Code == request.Code)
            .ToListAsync(cancellationToken);
        return Result.From(items.Adapt<List<SysRoleDto>>());
    }
}
